package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.R;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

public class SalaryPopWindow extends BasePopWindow implements View.OnClickListener {
    private RadioButton timeH, timeDay, timeMonth;
    private TextView okView, resetView;
    private RadioGroup radioGroup;
    private EditText inputData;
    private String dataTxt, unitTxt = "小时", unitNo = "1";
    private int maxLength = 5;

    public SalaryPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    public SalaryPopWindow(Context context,int maxLength) {
        super(context, DIR_DOWN_UP);
        this.maxLength = maxLength;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_salary;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        inputData = holder.getPop().findViewById(R.id.input_data);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        radioGroup = holder.getPop().findViewById(R.id.rd_rp);
        timeH = holder.getPop().findViewById(R.id.time_h);
        timeDay = holder.getPop().findViewById(R.id.time_day);
        timeMonth = holder.getPop().findViewById(R.id.time_month);
        okView.setOnClickListener(this);
        resetView.setOnClickListener(this);
        inputData.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    inputData.setHint("请输入薪资标准");
                    return;
                }

                if ("0".equals(s.toString())) {
                    inputData.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (maxLength > 0){
                    if (inputData.getText().toString().length() > maxLength){
                        inputData.setText(inputData.getText().toString().substring(0,maxLength));
                    }
                }
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.time_month:
//                        unitTxt = "月";
                        unitTxt = timeMonth.getText().toString();
                        unitNo = "3";
                        break;
                    case R.id.time_day:
//                        unitTxt = "天";
                        unitTxt = timeDay.getText().toString();
                        unitNo = "2";
                        break;
                    case R.id.time_h:
//                        unitTxt = "小时";
                        unitTxt = timeH.getText().toString();
                        unitNo = "1";
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ok_view:
                if (onResultClick != null) {
                    if (TextUtils.isEmpty(inputData.getText().toString().trim())) {
                        Toast.makeText(mContext, "请输入薪资标准", Toast.LENGTH_SHORT).show();
                    } else {
                        onResultClick.result(inputData.getText().toString(), unitTxt, unitNo);
                        dismiss();
                    }
                }
                break;
            case R.id.reset_view:
                cancel();
                dismiss();
                break;
            default:
                break;
        }
    }

    private void cancel() {
        inputData.setText("");
        timeH.setChecked(true);
        unitTxt = "小时";
        unitNo = "1";
        onResultClick.result(inputData.getText().toString(), "", "0");
    }

    public void setPopData(String dataTxt, String unitTxt) {
        this.dataTxt = dataTxt;
        this.unitTxt = unitTxt;
        if (unitTxt == null) {
            this.unitTxt = "小时";
        }
        if ("0".equals(dataTxt)) {
            this.dataTxt = "";
        }
        switch (this.unitTxt) {
            case "小时":
                unitNo = "1";
                timeH.setChecked(true);
                break;
            case "天":
                unitNo = "2";
                timeDay.setChecked(true);
                break;
            case "月":
                unitNo = "3";
                timeMonth.setChecked(true);
                break;
            default:
                break;
        }
        inputData.setText(dataTxt);
    }

    /**
     * 设置数据
     * @param unit 后面是一个数组，最大是三个，就算写了三个以上也没有用，因为根本就只会取到三个，分别对应后面的三个提示，小时，天，月，三个选择框的文字提示，这里这样设计是为了应对不同需求
     */
    public void setPopDataChange(String...unit) {
        if (unit.length > 0){
            timeH.setText(unit[0]);
        }

        if (unit.length > 1){
            timeDay.setText(unit[1]);
        }

        if (unit.length > 2){
            timeMonth.setText(unit[2]);
        }

        unitTxt = unit[0];
    }


    IDisMiss iDisMiss;

    public interface IDisMiss {
        void OnClose();
    }

    public void OnDisMiss(IDisMiss iDisMiss) {
        this.iDisMiss = iDisMiss;
    }

}
